const daoCommon = require('../utils/daoCommon');
const sqlHelper=require('../utils/sqlHelper');

const dao = {
    /**
     * 新增课程
     * */
    AddCurriculum: async (data) => {
        let sql = `insert into curriculum (CurriculumId,CurriculumName,StageTitle,TeacherUserId,CurriculumDescribe,StageState,Price,CreationTime) 
        values ($CurriculumId,$CurriculumName,$StageTitle,$TeacherUserId,$CurriculumDescribe,$StageState,$Price,NOW())`;
        return await daoCommon.oneMethod(sql, data)
    },
    /**
     * 课程信息查询
     * */
    QueryCurriculum: async (data) => {
        let sql = `select (select TeacherUserName from teacheruser where TeacherUserId=$TeacherUserId) as TeacherUserName,
        DATE_FORMAT(CreationTime, '%Y-%m-%d' ) as CreationTime,CurriculumId,CurriculumName,StageTitle,CurriculumDescribe,StageState,Price
        from curriculum where TeacherUserId=$TeacherUserId order by CreationTime desc limit ${data.page - 1},${data.size}`;
        return await daoCommon.oneMethod(sql, data)
    },
    /**
     * 课程信息查询总条数
     * */
    QueryCurriculumCount: async (data) => {
        let sql = `select count(*) as Count from curriculum where TeacherUserId=$TeacherUserId`;
        return await daoCommon.oneMethod(sql, data)
    },
    /**
     * 修改课程信息状态
     * */
    UpdateCurriculum: async (data) => {
        let sql = `update curriculum set StageState=$StageState where CurriculumId=$CurriculumId`;
        return await daoCommon.oneMethod(sql, data)
    },
    /**
     * 添加课程详情
     * */
    AddCurriculumDetails: async (data)=>{
        let sqls = [];
        let CurriculumData=data.CurriculumData;
        data.CurriculumDetailsList.forEach((CurriculumDetailsList)=>{
            let sql=`insert into curriculumdetails (CurriculumDetailsId,CurriculumId,Section,SectionTitle,Duration,EnrolmentNumber,ListensNumber,TeachingCondition,OpeningHours) 
            value ($CurriculumDetailsId,'${CurriculumData.CurriculumId}',$Section,$SectionTitle,$Duration,${0},${0},${0},FROM_UNIXTIME(${CurriculumDetailsList.OpeningHours/1000}))`;
            sqls.push(sqlHelper.getNewSql(sql, CurriculumDetailsList));
        });
        let sql2=`update curriculum set StageState=${1} where CurriculumId=$CurriculumId`;
        sqls.push(sqlHelper.getNewSql(sql2, CurriculumData));
        return sqlHelper.execTrans(sqls);
    },
    /**
     * 查询课程详情
     * */
    QueryCurriculumDetails:async (data)=>{
        let sql = `select * from curriculumdetails where CurriculumId=$CurriculumId order by Section asc`;
        return await daoCommon.oneMethod(sql, data)
    }
};

module.exports = dao;
